package com.dingding.activity.core.activity.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.activity.core.activity.dao.ICouponGrantRecordCacheDao;
import com.dingding.activity.core.activity.dao.ICouponGrantRecordDao;
import com.dingding.common.core.base.daox.AbstractBaseInjectionDao;
import com.dingding.common.core.beans.activity.CouponGrantRecord;
import com.dingding.common.core.cache.CacheConstants;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.common.core.util.JSONUtil;

/**
 * 
 * @Description: 优惠券发放记录DAO
 * @author CHEHUANBO
 * @date 2016年12月12日
 * @version V2.6.2
 * 
 */
@Component("couponGrantRecordDao")
public class CouponGrantRecordDaoImpl extends AbstractBaseInjectionDao
		implements ICouponGrantRecordDao, ICouponGrantRecordCacheDao {

	private static final Logger log = Logger.getLogger(CouponGrantRecordDaoImpl.class);

	/**
	 * 
	 * @Description: 添加优惠券发放记录表
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = RuntimeException.class)
	public void addCouponGrantRecord(CouponGrantRecord couponGrantRecord) throws Exception {

		super.getBaseDaoX().save(couponGrantRecord);

	}

	/**
	 * 
	 * @Description: 获取优惠券发放记录
	 * @param couponId
	 *            优惠券ID
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public List<CouponGrantRecord> getCouponGrantRecordToDb(int couponId) throws RuntimeException {

		if (couponId == 0) {
			throw new NullPointerException("获取优惠券发放记录被拒绝，原因：couponId is null");
		}

		Criterion criterion = Restrictions.eq("couponId", couponId);

		List<CouponGrantRecord> recordList = super.getCriteriaBaseDao().queryCriteriaList(CouponGrantRecord.class,
				criterion);

		return recordList;
	}

	/**
	 * 
	 * @Description: 从缓存中获取当前优惠券发放数量
	 * @param couponId
	 *            优惠券ID
	 * @return long 当前优惠券已发放数量
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public long getCouponGrantRecordToCacheByCouponId(int couponId) {

		String couponGrantRecordKey = CacheConstants.COUPON_GRANT_RECORD_LIST_ + couponId;

		return JedisUtil.llen(couponGrantRecordKey, DbEnum.ActivityDb.getValue());

	}

	/**
	 * 
	 * @Description: 从缓存中获取当前优惠券记录
	 * @param activityId
	 *            活动ID
	 * @param userId
	 *            用户ID
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	public CouponGrantRecord getCouponGrantRecordToCacheByCouponId(String activityId, String userId) {

		String couponGrantRecordKey = CacheConstants.COUPON_GRANT_RECORD_LIST_ + activityId + userId;

		try {
			String cacheValue= JedisUtil.getValue(couponGrantRecordKey,
					DbEnum.ActivityDb.getValue());
			return JSONUtil.parseObject(cacheValue,CouponGrantRecord.class);

		} catch (Exception e) {
			log.error("从缓存中获取当前优惠券记录出现异常！", e);
		}

		return null;

	}

	/**
	 * 
	 * @Description: 根据活动ID和用户ID获取活动记录
	 * @param activityId
	 *            活动ID
	 * @param userId
	 *            用户ID
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Transactional(readOnly = true)
	public CouponGrantRecord getCouponGrantRecordToDb(String activityId, String userId) throws RuntimeException {

		if (StringUtils.isBlank(activityId) || StringUtils.isBlank(userId)) {
			throw new NullPointerException("根据活动ID和用户ID获取活动记录被拒绝！");
		}

		Criterion criterion = Restrictions.and(Restrictions.eq("activityId", activityId),
				Restrictions.eq("userId", userId));
		CouponGrantRecord couponGrantRecord = super.getCriteriaBaseDao()
				.queryCriteriaListUnique(CouponGrantRecord.class, criterion);

		return couponGrantRecord;

	}

}
